package gw.com.android.ui.onlive;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.gwtsz.gts2.hx.R;

import java.util.Set;

import gw.com.android.presenter.http.DiscussRequest;

/**
 * 会闪烁的VIP 图标
 * <p>
 * 需要结合layout_vip_animator.xml来使用
 * <p>
 * 这里包含了这样一个逻辑“1、拉到屏幕范围内开始播放；2、如果已播放后不在屏幕内，再拉回至屏幕，不需要再循环一次动画。”
 * 所以看到奇怪的代码，按这个逻辑来想想
 */
public class VipAnimatorLayout extends RelativeLayout {

    private final static String TAG = "VipAnimatorLayout";

    public VipAnimatorLayout(Context context) {
        this(context, null);
    }

    public VipAnimatorLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VipAnimatorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private String vipId;
    private int tradeType;

    public void setVipId(int tradeType, String vipId) {
        this.tradeType = tradeType;
        this.vipId = vipId;
        executeAttach();
    }

    private boolean ifAttached = false;

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        ifAttached = true;
        executeAttach();
    }


    private void executeAttach() {
        if (getVisibility() != VISIBLE) return;
        if (ifAttached && vipId != null) {
            Set<String> vipList;
            if (DiscussRequest.TRADE_TYPE_GUEST == tradeType) {
                vipList = QuanziListFragmentGuest.vipListGuest;
            } else if (DiscussRequest.TRADE_TYPE_ALL_POST == tradeType) {
                vipList = QuanziListFragmentPost.vipListPost;
            } else if (DiscussRequest.TRADE_TYPE_BEST == tradeType) {
                vipList = QuanziListFragmentBest.vipListBest;
            } else {
                Log.d(TAG, "错误类型:" + tradeType);
                return;
            }

            Log.d(TAG, "vipList:" + vipList.toString());
            Log.d(TAG, "vipId:" + vipId);
            if (vipList.contains(vipId)) {//如果包含就说明进行过动画
                Log.d(TAG, " --动画已进行过--  " + vipId + "-" + this.hashCode());
            } else {
                vipList.add(vipId);
                Log.d(TAG, vipList.toString() + " --这条帖子没有执行过动画--  " + vipId + "-" + this.hashCode());
                initCurrentCount();
                startVipAnimator();
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (getVisibility() != VISIBLE) return;
        Log.d(TAG, "onDetachedFromWindow:" + this.hashCode());
        vipId = null;
        ifAttached = false;
        if (setAll != null)//离开视线，停止动画
            setAll.cancel();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    private ImageView iv_ray;
    private ImageView iv_star1, iv_star2;
    private Handler handler = new Handler();

    private void init() {
        iv_ray = (ImageView) findViewById(R.id.iv_ray);
        iv_star1 = (ImageView) findViewById(R.id.iv_star1);
        iv_star2 = (ImageView) findViewById(R.id.iv_star2);
        iv_ray.setVisibility(View.INVISIBLE);
    }

    AnimatorSet setAll;
    private int currentCount = 0;//当前执行了几次
    private boolean ifAnimating = false;

    public boolean isIfAnimating() {
        return ifAnimating;
    }

    public void initCurrentCount() {
        currentCount = 0;
    }

    public void startVipAnimator() {
        if (currentCount >= 2) {
            return;
        }
        AnimatorSet setRay = new AnimatorSet();
        int duration = 600;
        int disX = 25;
        int disY = 25;
        ObjectAnimator animatorRayX = ObjectAnimator.ofFloat(iv_ray, "translationX", -disX, disX).setDuration(duration);
        ObjectAnimator animatorRayY = ObjectAnimator.ofFloat(iv_ray, "translationY", -disY, disY).setDuration(duration);
        AnimatorListenerAdapter animatorListenerAdapter = new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                iv_ray.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                iv_ray.setVisibility(View.VISIBLE);
            }
        };
        animatorRayX.addListener(animatorListenerAdapter);
        setRay.playTogether(animatorRayX, animatorRayY);
        setRay.setStartDelay(100);

        setAll = new AnimatorSet();
        // 星星的闪烁，居然是透明度的变化
        // 然后，是星星
        ObjectAnimator animatorStar1 = ObjectAnimator.ofFloat(iv_star1, "alpha", 1, 0).setDuration(200);
        animatorStar1.setStartDelay(300);
        ObjectAnimator animatorStar2 = ObjectAnimator.ofFloat(iv_star2, "alpha", 1, 0).setDuration(200);
        setAll.playSequentially(setRay, animatorStar1, animatorStar2); //这特么不是顺序执行么？怎么感觉不对

        setAll.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
                ifAnimating = false;
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                //需要重复两遍
                if (currentCount < 2) {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            startVipAnimator();
                        }
                    }, 500);//但是第二遍需要延迟
                } else {
                    ifAnimating = false;
                }
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                currentCount++;
                ifAnimating = true;
            }
        });
        setAll.start();
    }
}
